<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->
    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-show="loading"/>
    <!-- /加载中 loading -->
    <div class="detail" v-if='is404'>文章不见了</div>
    <!-- 文章详情 -->
    <div class="detail"  v-else >
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="article.aut_photo" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | dateFormat}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="ToSureFocus"
        >{{article.is_followed ? '已关注' : '关注'}}</van-button>
      </div>
      <div class="content">
        <div v-html='article.content'></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
        round size="small"
        hairline type="primary"
        plain icon="good-job-o"
        @click="toSureOK">
        {{article.attitude === 1 ? '取消点赞' : '点赞'}}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
        round
        size="small"
        hairline
        type="danger"
        plain
        icon="delete"
        @click="toSureLove"
        >{{article.attitude === 1 ? '取消不喜欢' : '不喜欢'}}</van-button>
      </div>
    </div>
    <!-- /文章详情 -->
    <!-- 评论区 -->
    <article-comment :articleId='$route.params.id'></article-comment>
  </div>
</template>

<script>
import { getArticles, delLike, addLike, loveArticle, hateArticle } from '@/api/article'
import { focusAuthor, NofocusAuthor } from '@/api/user'
import articleComment from './articleComment'
export default {
  name: 'ArticleIndex',
  components: {
    articleComment
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/login' && this.$store.state.tokenInfo.token) {
      next('/')
    } else {
      next()
    }
  },
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {},
      is404: false
    }
  },
  created () {
    this.loadArticle()
  },
  methods: {
    // 获取文章详情内容 渲染数据
    async loadArticle () {
      try {
        const res = await getArticles(this.$route.params.id)
        this.article = res.data.data
        this.loading = false
      } catch (err) {
        this.loading = false
        if (err && err.response && err.response.status === 404) {
          this.is404 = true
        } else {
          this.$toast.fail('文章加载失败')
        }
      }
    },
    // 关注或者取消关注作者
    async ToSureFocus () {
      // 判断如果关注了 就取消关注
      if (this.article.is_followed) {
        this.article.is_followed = false
        await NofocusAuthor(this.article.aut_id.toString())
        this.$toast.success('取消关注成功')
        // 如果没有关注的话
      } else {
        this.article.is_followed = true
        await focusAuthor(this.article.aut_id.toString())
        this.$toast.success('关注成功')
      }
    },
    // 点赞或者取消点赞
    async toSureOK () {
      if (this.article.attibute === 1) {
        await delLike(this.article.aut_id.toString())
      } else {
        await addLike(this.article.aut_id.toString())
      }
      // 点击之后将attitude的值进行取反就可以了
      this.article.attitude = this.article.attitude * -1
    },
    // 喜欢不喜欢
    async toSureLove () {
      if (this.article.attibute === 0) {
        await loveArticle(this.article.art_id.toString())
        this.article.attitude = -1
      } else {
        await hateArticle(this.article.art_id.toString())
        this.article.attitude = 0
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
